package cn.trda.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import cn.trda.mapper.AsRolePremissionMapper;
import cn.trda.pojo.RolePremission;
import cn.trda.service.PermissionService;
@Service
public class PermissionServiceImpl implements PermissionService {
	private static final Logger logger = Logger.getLogger(PermissionServiceImpl.class);
	@Autowired
	private AsRolePremissionMapper asRolePremissionMapper;

	@Override
	public int insert(RolePremission record) {
		// TODO Auto-generated method stub
		return asRolePremissionMapper.insert(record);
	}

	@Override
	public int deleteByRoleId(Long id) throws Exception {
		return asRolePremissionMapper.deleteByRoleId(id);
	}
	@Override
	public List<RolePremission> selectByRoleid(Long roleid) {
		// TODO Auto-generated method stub
		logger.debug("数据库查询role_permissioin ---roleid: "+roleid);
		return asRolePremissionMapper.selectByRoleid(roleid);
	}
	@Override
	@CacheEvict(value={"role_permission"},allEntries=true)
	public void tx_delAddPremission(RolePremission pm, String flist) throws Exception{
		//System.out.println(flist);
		logger.debug("修改权限开始：当前方法传入的功能id列表值：" + flist);
		this.deleteByRoleId(pm.getRoleid());
		
		logger.debug("删除旧权限成功！！");
		if (!flist.trim().isEmpty()) {
			String[] ids = flist.split(",");
			for (int i = 0; i < ids.length; i++) {
				pm.setFunctionid(Long.valueOf(ids[i]));
				this.insert(pm);
				logger.debug("插入新权限成功！！功能id："+ids[i]);
			}
		}
	}
}
